package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.minordeath;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.QuotaConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.LcPolModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.ProcessModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.AcceptRequestDataModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.service.calculate.DeathLiabilityInsuranceService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.collections4.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * description:
 * 未成年人死亡责任累积保额基类
 *
 * @author jackdaw
 * @date 2020-01-06 15:24
 */
public class BasicMinorDeath {
    public static ProcessModel checkDeathLiabilityByRiskCode(AcceptRequestDataModel acceptRequestDataModel, ProductModel productModel,
                                                             RedisService redisService, DeathLiabilityInsuranceService deathLiabilityInsuranceService) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        //产品被保人序号
        int prodInsSeq = productModel.getProdInsSeq();
        //生效日
        Date valiDate = requestModel.getPolicyInfo().getCvalidate();
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        //被保人
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(prodInsSeq, insurantModelList);
        //生日
        Date birthday = insurantModel.getBirthday();
        //获取日期相差的天数和年
        int days = DateUtil.getDays(birthday, valiDate);
        int years = DateUtil.getYears(birthday, valiDate);
        //计算当前单
        BigDecimal currentAmount = deathLiabilityInsuranceService.currentDeathLiabilityAmnt(prodInsSeq, years, requestModel.getProductInfo());
        //缓存单
        BigDecimal cacheTotal = redisService.getCacheQuota(QuotaConstant.SORT_D, insurantModel.getIdNo(), currentAmount, requestModel.getPolicyInfo().getContNo());
        //计算历史单
        BigDecimal historyAmount = BigDecimal.valueOf(0);
        List<LcPolModel> lcPolModels = deathLiabilityInsuranceService.queryHistoryPolicy(requestModel.getPolicyInfo().getPrtNo(), insurantModel.getInsuredNo());
        if (CollectionUtils.isNotEmpty(lcPolModels)) {
            historyAmount = deathLiabilityInsuranceService.historyDeathLiabilityAmnt(lcPolModels);
        }

        ProcessModel processModel = new ProcessModel();
        processModel.setDay(days);
        processModel.setYear(years);
        processModel.setAmount(currentAmount.add(cacheTotal).add(historyAmount));
        return processModel;
    }
}
